package com.abou.mvc.controller.admin.product;

import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.abou.manager.exception.ManagerException;
import com.abou.model.common.product.Product;
import com.abou.mvc.controller.ACommonController;
import com.abou.mvc.controller.ControllerUtils;
import com.abou.mvc.controller.admin.HomeController;

/**
 * The controller for the home page
 * 
 * @author khalid
 */

@Controller
@RequestMapping("admin/product/modify.do")
public class ModifyProductController extends ACommonController {
  
  /**
   * The request mapping for modify product view
   */
  public static final String MODIFY_VIEW_REQUEST_MAPPING =
      "admin/product/modify.do";
  
  /**
   * The modify product view
   */
  public static final String MODIFY_VIEW = "admin/product/modify";
  
  /**
   * The constant defining the name of the product model attribute
   */
  public static final String PRODUCT_ATT = "product";
  
  /**
   * The constant defining the name of the product model attribute
   */
  public static final String IMAGE_ATT = "image";
  
  /**
   * The empty constructor
   */
  public ModifyProductController() {

    super(MODIFY_VIEW, HomeController.VIEW_NAME);
  }
  
  /**
   * Shows the home page.
   * 
   * @param productId The product ID
   * @param model The model
   * @return The name of the view to display
   */
  @RequestMapping(method = RequestMethod.GET)
  public @ModelAttribute(PRODUCT_ATT)
  Product show(@RequestParam(required = false) Integer productId, ModelMap model) {

    Product product = null;
    
    if (productId != null) {
      
      product = getProductManager().getProduct(productId);
      
    } else {
      
      product = (Product) model.get(PRODUCT_ATT);
    }
    
    return product;
  }
  
  /**
   * Receives the form data and handles it.
   * 
   * @param product The product to modify
   * @param result The binding result
   * @param model The model
   * @return The view to display
   * @throws Exception
   */
  @RequestMapping(method = RequestMethod.POST)
  public String submitForm(@ModelAttribute(PRODUCT_ATT) @Valid Product product,
      BindingResult result, ModelMap model) throws Exception {

    // the view to display
    String targetView = getViewName();
    
    if (!result.hasErrors()) {
      
      try {
        
        // updating the project
        getProductManager().updateProduct(product,
            getHttpRequest().getSession().getServletContext());
        
        // redirecting to the modify page with confirmation message
        targetView =
            ControllerUtils.REDIRECT_INSTR + HomeController.VIEW_NAME
                + ControllerUtils.VIEW_NAME_SUFFIX + ControllerUtils.PARAMS_DEL
                + ControllerUtils.PRODUCT_ID_ATT
                + ControllerUtils.PARAMS_VAL_SEP + product.getId()
                + ControllerUtils.PARAMS_SEP + ControllerUtils.CONFIRMATION_ATT
                + ControllerUtils.PARAMS_VAL_SEP
                + HomeController.PRODUCT_MODIFICATION_CONFIRMATION;
        
      } catch (ManagerException ex) {
        
        result.rejectValue(ex.getMessage(), "FileAlreadyExist");
        model.put(PRODUCT_ATT, product);
      }
      
    } else {
      
      model.put(PRODUCT_ATT, product);
    }
    
    return targetView;
  }
}
